// Libmagic.cs created with MonoDevelop
// User: xmonader at 1:41 PMÂ 4/15/2009

       //Libmagic.cs
       
       //Copyright 2009 ahmed youssef <xmonader@gmail.com>
       
       //This program is free software; you can redistribute it and/or modify
       //it under the terms of the GNU General Public License as published by
       //the Free Software Foundation; either version 2 of the License, or
       //(at your option) any later version.
       
       //This program is distributed in the hope that it will be useful,
       //but WITHOUT ANY WARRANTY; without even the implied warranty of
       //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       //GNU General Public License for more details.
       
       //You should have received a copy of the GNU General Public License
       //along with this program; if not, write to the Free Software
       //Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
       //MA 02110-1301, USA.



// To change standard headers go to Edit->Preferences->Coding->Standard Headers
//
// Imported on 2013-07-14 by kbeckman from
// https://bitbucket.org/xmonader/libmagicbindings/raw/94babbed55112a6f8e5e6c0ce84a53dd35058d2b/sharpmagic/sharpmagic/Libmagic.cs

using System;
using System.Text;
using System.Runtime.InteropServices;
using Mono;


namespace SharpMagic
{

	public class Libmagic
	{
		public enum MagicConstants : int{
		 MAGIC_NONE=0x000000, 			    // No flags 
		 MAGIC_DEBUG=0x000001 		        // Turn on debugging 
		,MAGIC_SYMLINK=0x000002 			// Follow symlinks 
		,MAGIC_COMPRESS=0x000004			// Check inside compressed files 
		,MAGIC_DEVICES=0x000008	 			// Look at the contents of devices 
		,MAGIC_MIME_TYPE=0x000010			// Return only the MIME type 
		,MAGIC_CONTINUE=0x000020 			// Return all matches 
		,MAGIC_CHECK=0x000040 				// Print warnings to stderr 
		,MAGIC_PRESERVE_ATIME=0x000080		// Restore access time on exit 
		,MAGIC_RAW=0x000100					// Don't translate unprint chars 
		,MAGIC_ERROR=0x000200 				// Handle ENOENT etc as real errors 
		,MAGIC_MIME_ENCODING=0x000400 		// Return only the MIME encoding 
		,MAGIC_MIME=(MAGIC_MIME_TYPE|MAGIC_MIME_ENCODING)
		,MAGIC_NO_CHECK_COMPRESS=0x001000 	// Don't check for compressed files 
		,MAGIC_NO_CHECK_TAR=0x002000 		// Don't check for tar files 
		,MAGIC_NO_CHECK_SOFT=0x004000 		// Don't check magic entries 
		,MAGIC_NO_CHECK_APPTYPE=0x008000	// Don't check application type 
		,MAGIC_NO_CHECK_ELF=0x010000		// Don't check for elf details 
		,MAGIC_NO_CHECK_ASCII=0x020000 		// Don't check for ascii files 
		,MAGIC_NO_CHECK_TOKENS=0x100000 	// Don't check ascii/tokens 
	
	    // Defined for backwards compatibility; do nothing 
		,MAGIC_NO_CHECK_FORTRAN=0x000000 	// Don't check ascii/fortran 
		,MAGIC_NO_CHECK_TROFF=0x000000 	    // Don't check ascii/troff 
		}
		public Libmagic()
		{
		}
		[DllImport("libmagic", EntryPoint="magic_open")]
		public static extern IntPtr MagicOpen(int i);
		
		[DllImport("libmagic", EntryPoint="magic_close")]
		public static extern void MagicClose(IntPtr magic_cookie);
		
		[DllImport("libmagic", EntryPoint="magic_file")]
		public static extern IntPtr MagicFile(IntPtr magic_cookie, string p);
		
		[DllImport("libmagic", EntryPoint="magic_descriptor")]
		public static extern IntPtr MagicDescriptor(IntPtr magic_cookie, string sb, int size);
		
		[DllImport("libmagic", EntryPoint="magic_buffer")]
		public static extern IntPtr MagicBuffer(IntPtr magic_cookie, string p, int size);
		
		[DllImport("libmagic", EntryPoint="magic_error")]
		public static extern IntPtr MagicError(IntPtr magic_cookie);
		
		[DllImport("libmagic", EntryPoint="magic_setflags")]
		public static extern int MagicSetFlags(IntPtr magic_cookie, int i);
		
		[DllImport("libmagic", EntryPoint="magic_load")]
		public static extern int MagicLoad(IntPtr magic_cookie, string sb);
		
		[DllImport("libmagic", EntryPoint="magic_compile")]
		public static extern int MagicCompile(IntPtr magic_cookie, string sb);
		
		[DllImport("libmagic", EntryPoint="magic_check")]
		public static extern int MagicCheck(IntPtr magic_cookie, string sb);
		
		[DllImport("libmagic", EntryPoint="magic_errno")]
		public static extern int MagicErrno(IntPtr magic_cookie);
		
		public static string PtrToAnsiString(IntPtr p){
			return Marshal.PtrToStringAnsi(p);
		}
		public static string Guess(string filename, int flags){
			IntPtr magic_cookie=Libmagic.MagicOpen(flags);
			Libmagic.MagicLoad(magic_cookie, null);
			string res=Libmagic.PtrToAnsiString(Libmagic.MagicFile(magic_cookie, filename));
			Libmagic.MagicClose(magic_cookie);	
			return res;
		}
	}
}
